//
//  TTHttpRequest.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/6/3.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "AFNetworking.h"
#import "TTHttpRequest.h"
#import "TTUDIDManager.h"
#import "NSDate+InternetDateTime.h"

@implementation TTHttpRequest

- (void)getStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    manager.requestSerializer.timeoutInterval = 10.0;
//    [manager.requestSerializer setValue:[TTUserModel sharedObject].accessToken forHTTPHeaderField:@"Authorization"];
//    [manager.requestSerializer setValue:@"application/text;charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
//    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
//    [manager.requestSerializer setValue:APP_VERSION forHTTPHeaderField:@"x-version"];
//    [manager.requestSerializer setValue:@"store" forHTTPHeaderField:@"x-client"];
//    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"x-equCode"];
//    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    
//    WEAKSELF
    [manager GET:self.url parameters:self.getData progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
        self.finishBlock(jsonDict);
        
//        BOOL isLogin = [[jsonDict valueForKey:@"login"] boolValue];
//        if (!isLogin && ![weakSelf.url containsString:@"/ipad/login"]) {
//            [weakSelf goLoginWithMessage:[jsonDict valueForKey:@"message"]];
//        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.failedBlock(error);
    }];
    
}

- (void)postStart {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 10.0;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];

    [manager.requestSerializer setValue:[TTUserModel sharedObject].accessToken forHTTPHeaderField:@"Authorization"];
    [manager.requestSerializer setValue:@"iOS" forHTTPHeaderField:@"x-platform"];
    [manager.requestSerializer setValue:APP_VERSION forHTTPHeaderField:@"x-version"];
    [manager.requestSerializer setValue:[TTUDIDManager getUniqueDeviceID] forHTTPHeaderField:@"DeviceNumber"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
    [manager POST:self.url parameters:self.postData progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *jsonDict = [Utilities dictionaryWithJsonString:responseObject];
        self.finishBlock(jsonDict);
        [self refreshTimeWithTask:task AndID:responseObject];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD dismiss];
            self.failedBlock(error);
    }];
    
}

- (void)refreshTimeWithTask:(NSURLSessionDataTask * _Nonnull) task AndID:(id _Nullable)responseObject {
    
    NSHTTPURLResponse *response = (NSHTTPURLResponse *)task.response;
    NSDictionary *allHeaders = response.allHeaderFields;
    NSString *dateServer = [allHeaders valueForKey:@"Date"];
    NSDate *inputDate = [NSDate dateFromRFC822String:dateServer];
    NSTimeInterval serverTimeStamp = [Utilities getTimestampWithDate:inputDate];
    [[NSUserDefaults standardUserDefaults] setDouble:serverTimeStamp forKey:@"kLastSaveServerTimeStamp"];
    NSTimeInterval currentLocalTimeStamp = [Utilities getCurrentTimestamp];
    
    [[NSUserDefaults standardUserDefaults] setDouble:currentLocalTimeStamp forKey:@"kLastSaveLocalTimeStamp"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//- (void)goLoginWithMessage:(NSString *)msg {
//    
//    [[TTUserModel sharedObject] initialization]; //userModel清空当前用户的所有信息
//    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:[NSClassFromString(@"PPLoginViewController") new]];
//    [UIApplication sharedApplication].keyWindow.rootViewController = navController;
//    navController.navigationBar.hidden = YES;
//}

@end
